package com.zlt.controller.seeker;

import com.alibaba.fastjson.JSON;
import com.zlt.controller.BaseController;
import com.zlt.service.InterviewService;
import com.zlt.service.impl.InterviewServiceImpl;
import com.zlt.util.JWTUtil;
import com.zlt.vo.Result;
import com.zlt.vo.ViewInterviewVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//面试管理
@WebServlet("/seeker/interview/*")
public class InterviewController extends BaseController {

    private InterviewService interviewService = new InterviewServiceImpl();

    @Override
    protected void defaultMethod(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { }

    //查看面试信息
    protected Result show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        try {
//            boolean success = interviewService.showInfo(req.getHeader("token"));
            List<ViewInterviewVo> success = interviewService.showInfo(req.getHeader("token"));

            return !success.isEmpty() ? Result.ok("show成功", JSON.toJSON(success)) : Result.error("show失败");
        } catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    //接受或者拒绝
    protected Result aor(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
        try {
            String username = JWTUtil.getUsername(req.getHeader("token"));
            String state =  req.getParameter("aor");
            String id =  req.getParameter("id");
            boolean success = interviewService.AcceptOrReject(username,id,state);
            return success ? Result.ok("aor成功") : Result.error("aor失败");
        } catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
}
